package de.uni_mannheim.informatik.ai.dame_ai.dame.ui;

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;

import de.uni_mannheim.informatik.ai.dame_ai.dame.domain.Player;

public class SidePanel extends JPanel{
	JLabel[] playerLabels;
	boolean replay;
	JPanel playersPane;
	JPanel statisticPane;
	JPanel replayPane;
	Player[] players;
	BufferedImage[] pieces;
	DameGui parent;
	public SidePanel(Rectangle bounds, boolean isReplay, Player[] players, BufferedImage[] pieces, DameGui parent){
		this.replay = isReplay;
		this.pieces = pieces;
		this.players = players;
		this.parent = parent;
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		setBounds(bounds);
		this.setBounds(bounds);
		this.setBackground(Color.LIGHT_GRAY);
		this.setOpaque(true);
		initSidepanel();
	}
	public void recreate(){
		this.removeAll();
		initSidepanel();
	}
	private void initSidepanel(){
		add(Box.createRigidArea(new Dimension(0,10)));
		JLayeredPane layeredPane = new JLayeredPane();
		layeredPane.setMaximumSize(new Dimension(getWidth(),10));
		layeredPane.setLayout(new BorderLayout());
		JLabel newLabel = new JLabel("PLAYERS");
		layeredPane.add(newLabel,BorderLayout.CENTER);
		add(layeredPane);
		initPlayersPane(75);
		add(Box.createRigidArea(new Dimension(0,10)));
		initStatisticPane();
		add(Box.createRigidArea(new Dimension(0,10)));
		if(replay)initReplayPane(50);
		validate();
	}
	
	@Override
	public void paint(Graphics g) {
		// TODO Auto-generated method stub
		super.paint(g);
	}
	
	private void initPlayersPane(int height){
		//playersPane = new JPanel(new BoxLayout(playersPane, BoxLayout.Y_AXIS));
		JLayeredPane layeredPane = new JLayeredPane();
		
		layeredPane = new JLayeredPane();
		layeredPane.setMaximumSize(new Dimension(getWidth(),height));
		layeredPane.setLayout(new GridLayout(2,2,5,5));
		int contentHeight = height - 3*5-10;
		int contentWidth = getWidth() -15 ;
		playerLabels = new JLabel[2];
		for(int i = 0;i<players.length;i++){
			playerLabels[i]=new JLabel(players[i].getName());
			layeredPane.add(playerLabels[i], new Integer(0));
			BufferedImage temp;
			//layeredPane.add(Box.createRigidArea(new Dimension(contentWidth/3,contentHeight/2)));
			if(players[i].getColor()== Player.BLACK){
				layeredPane.add(new customCircle(getBackground(),Color.black,contentHeight/2,contentHeight/2));
				
			}else {
				layeredPane.add(new customCircle(getBackground(),Color.white,contentHeight/2,contentHeight/2));
			}			
		}
		/*layeredPane.setVisible(true);
		playersPane.add(layeredPane);*/
		add(layeredPane);
		
		
		//playersPane.setVisible(true);
	}
	private void initStatisticPane(){
		
	}
	JButton nextMove;
	JButton lastMove;
	JButton automaticReplay;
	JLayeredPane layeredPane;
	private void initReplayPane(int height){
		 layeredPane = new JLayeredPane();
		layeredPane.setMaximumSize(new Dimension(getWidth(),height));
		layeredPane.setLayout(new GridLayout(2,2));
		lastMove = new JButton("Zurück");
		lastMove.addActionListener(new ActionListener() {
		   public void actionPerformed(ActionEvent ae) {
		      parent.lastMove();
		      System.out.println("Button event occured");
		   }
		}
		);
		layeredPane.add(lastMove);
		nextMove = new JButton("Weiter");
		
		nextMove.addActionListener(new ActionListener() {
			   public void actionPerformed(ActionEvent ae) {
			      parent.nextMove();
			      System.out.println("Button event occured");
			   }
			}
			);
		
		layeredPane.add(nextMove);
		automaticReplay = new JButton("Stop Auto Replay");
		automaticReplay.addActionListener(new ActionListener() {
			   public void actionPerformed(ActionEvent ae) {
			      
				  Boolean result = parent.toggleAutomaticReplay();
				  layeredPane.remove(automaticReplay);
			      if(result){
			    	  automaticReplay.setText("Stop Auto Replay");
			      } else {
			    	  automaticReplay.setText("Start Auto Replay");
			      }
			      layeredPane.add(automaticReplay);
			      System.out.println("Button event occured");
			   }
			}
			);
		layeredPane.add(automaticReplay);
		add(layeredPane);
		
		
	}
	
	class customCircle extends JPanel{
		Color circleColor;
		int width;
		int height;
		public customCircle(Color background, Color circleColor, int width, int height){
			setBackground(background);
			this.circleColor = circleColor;
			this.width = width;
			this.height = height;
			setMaximumSize(new Dimension(width,height));
		}

		@Override
		public void paint(Graphics g) {
			// TODO Auto-generated method stub
			super.paint(g);
			Graphics2D g2 = (Graphics2D) g;
		    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
		        RenderingHints.VALUE_ANTIALIAS_ON);

		    g2.setPaint(circleColor);
		    g2.fill(new Ellipse2D.Double(0, 0, width, height));
		}
		
		
	}
	
}
